package com.lagou.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author tengfei wang
 * @version 1.0
 * @date Created in 2020/8/31
 */
public class DruidsUtils {

    private DruidsUtils() {
    }

    private static final DruidsUtils instance = new DruidsUtils();

    public static DruidsUtils getInstance() {
        return instance;
    }

    public Connection getConnection() {
        Properties properties = PropertyUtil.getProperties();
        try {
            DruidDataSource druidDataSource = new DruidDataSource();
            assert properties != null;
            druidDataSource.setDriverClassName(properties.getProperty("jdbc.driverClassName"));
            druidDataSource.setUrl(properties.getProperty("jdbc.url"));
            druidDataSource.setUsername(properties.getProperty("jdbc.username"));
            druidDataSource.setPassword(properties.getProperty("jdbc.password"));
            return druidDataSource.getConnection();
        } catch (SQLException e) {
            System.out.println("获取数据库连接异常");
            e.printStackTrace();
        }
        return null;
    }
}
